module MC.Order {
	export class comCheck {
		public constructor() {
			this.init();
		}
		private init() {
			if ($('#check_uid').val() == '' && $('#type').val() === 'gs') {
				this.showxw();
			}
			this.out_trade_no = $('#out_trade_no').val();
			if ($('#check_uid').val() == '') {
				$('.comCheckDiv [type=text],.comCheckDiv textarea').attr('readonly', 'readonly');
				$('.comCheckDiv input,.comCheckDiv textarea').attr('placeholder', '');
				$('.comCheckDiv [type=radio]').attr({
					disabled: 'disabled',
					readonly: 'readonly'
				});
			} else {
				$('[name=finishTime],[name=period]').datepicker({
					dateFormat: "yy-mm-dd",
					dayNamesMin: ["一", "二", "三", "四", "五", "六", "日"],
					monthNamesShort: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
					changeMonth: true,
					changeYear: true,
					firstDay: 0,
					prevText: '上月',
					nextText: '下月'
				});

			}
			$('.comCheckDiv .saveDiv .savebutton').click(() => {
				this.save();
			});
		}
		private out_trade_no: string = '';
		private html: JQuery;
		private showxw() {
			this.html = $(`<div class="msgbody">
                <div class="msgBox">
                    <div class="boxcon">跟单验货报告是公司填写还是工厂填写?确定后无法再次修改</div>
                    <div class="butarray clearfix">
                        <a class="button gongchang">工厂</a>
                        <a class="button gongsi ">公司</a>
                    </div>
                </div>
			</div>`);
			this.html.find('.gongchang').click(() => {
				this.upqx('cid');
			});
			this.html.find('.gongsi').click(() => {
				this.upqx('fid');
			});
			$('.comCheckDiv').append(this.html);
		}
		private upqx(check_type: string) {
			MC.api('comCheckUid', {
				out_trade_no: this.out_trade_no,
				check_type: check_type
			}, (json: any) => {
				this.html.remove();
				//	MC.msg.show("设置成功");
				setTimeout(function () {
					location.reload();
				}, 1500);
			});
		}

		/**
		 * 保存表单数据
		 */
		private save() {
			let list = $('#stylelist [name]');
			let stylelist: any = {};
			for (let i = 0; i < list.length; i++) {
				//	let obj:any={};
				stylelist[$(list[i]).attr('name')] = $(list[i]).val();
				//	stylelist.push(obj);
			}


			list = $('#sizelist [name]');
			let sizelist: any = {};
			for (let i = 0; i < list.length; i++) {
				//let obj:any={};
				sizelist[$(list[i]).attr('name')] = $(list[i]).val();
				//	sizelist.push(obj);
			}

			list = $('#thrumlist [name][type=radio]:checked');
			let thrumlist: any = {};
			for (let i = 0; i < list.length; i++) {
				thrumlist[$(list[i]).attr('name')] = $(list[i]).val();
			}

			list = $('#thrumlist [name][type=text]');

			for (let i = 0; i < list.length; i++) {
				thrumlist[$(list[i]).attr('name')] = $(list[i]).val();
			}


			let detail = $('[name=detail]').val();
			let staffTotalNum = $('[name=staffTotalNum]').val();
			let staffStyleNum = $('[name=staffStyleNum]').val();
			let finishTime = $('[name=finishTime]').val();

			let com_name = $('[name=com_name]').val();
			let com_tel = $('[name=com_tel]').val();

			let obj = {
				stylelist: stylelist,
				sizelist: sizelist,
				thrumlist: thrumlist,


				detail: detail,
				staffTotalNum: staffTotalNum,
				staffStyleNum: staffStyleNum,
				finishTime: finishTime,

				com_name: com_name,
				com_tel: com_tel,
				out_trade_no: this.out_trade_no
			};
			MC.api('savecomCheck', obj, (json: any) => {
				MC.msg.show("保存成功");
			});
		}

	}
}